<template>
    <div>
        <div class="userManager">
            <div class="queryForm">
                <query-form
                    :form="searchLabel"
                    v-model="targetForm"
                    @searchHandler="search"
                    @cancelHandler="resetSearch"
                ></query-form>
                <!-- <el-form :model="targetForm" ref="targetRef" :inline="true">
                    <el-form-item label="菜单名字" label-width="100">
                        <el-input
                            v-model="targetForm.targetName"
                            autocomplete="off"
                            placeholder="请输入菜单名字"
                        />
                    </el-form-item>
                    <el-form-item label="菜单状态" label-width="100">
                        <el-select v-model="targetForm.targetState" placeholder="请输入菜单状态">
                            <el-option label="正常" value="1" />
                            <el-option label="停用" value="2" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label-width="100">
                        <el-button type="primary" @click="search">查询</el-button>
                        <el-button>重置</el-button>
                    </el-form-item>
                </el-form> -->
            </div>
            <div class="baseTable">
                <div class="action">
                    <el-button type="primary" @click="menuAdd(1)" v-has="'menu-create'">
                        新增
                    </el-button>
                </div>
                <el-table
                    ref="listRef"
                    :data="menuList"
                    style="width: 100%"
                    row-key="_id"
                    :tree-props="{ children: 'children' }"
                >
                    <el-table-column
                        v-for="ele in columns"
                        :key="ele.prop"
                        :prop="ele.prop"
                        :label="ele.label"
                        :formatter="ele.formatter"
                        :width="ele.width"
                    />
                    <el-table-column label="操作" min-width="120">
                        <template #default="scope">
                            <el-button
                                type="success"
                                @click="menuAdd(2, scope.row)"
                                :icon="Plus"
                                v-has="'menu-create'"
                            />
                            <el-button
                                type="primary"
                                @click="menuEdit(scope.row)"
                                :icon="Edit"
                                v-has="'menu-edit'"
                            />
                            <el-button
                                type="danger"
                                @click="menuDelete(scope.row)"
                                :icon="Delete"
                                v-has="'menu-delete'"
                            />
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <el-dialog v-model="showDialog" @close="close">
                <el-form :model="menuForm" ref="dialogForm" :rules="rules">
                    <el-form-item label="父级菜单" prop="parentId" label-width="100">
                        <el-cascader
                            v-model="menuForm.parentId"
                            :options="menuList"
                            :props="{ checkStrictly: true, value: '_id', label: 'menuName' }"
                            clearable
                            placeholder="请选择父级菜单"
                        />
                        <span>不选，默认设置为一级菜单</span>
                    </el-form-item>
                    <el-form-item label="菜单类型" prop="menuType" label-width="100">
                        <el-radio-group v-model="menuForm.menuType">
                            <el-radio :label="1">菜单</el-radio>
                            <el-radio :label="2">按钮</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="菜单名称" prop="menuName" label-width="100">
                        <el-input v-model="menuForm.menuName" autocomplete="off" />
                    </el-form-item>
                    <el-form-item
                        label="菜单图标"
                        prop="icon"
                        label-width="100"
                        v-show="menuForm.menuType == 1"
                    >
                        <el-input v-model="menuForm.icon" autocomplete="off" />
                    </el-form-item>
                    <el-form-item
                        label="路由地址"
                        prop="path"
                        label-width="100"
                        v-show="menuForm.menuType == 1"
                    >
                        <el-input v-model="menuForm.path" autocomplete="off" />
                    </el-form-item>
                    <el-form-item
                        label="组件路径"
                        prop="component"
                        label-width="100"
                        v-show="menuForm.menuType == 1"
                    >
                        <el-input v-model="menuForm.component" autocomplete="off" />
                    </el-form-item>
                    <el-form-item
                        label="菜单状态"
                        prop="menuState"
                        label-width="100"
                        v-show="menuForm.menuType == 1"
                    >
                        <el-radio-group v-model="menuForm.menuState">
                            <el-radio :label="1">正常</el-radio>
                            <el-radio :label="2">停用</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item
                        label="权限标识"
                        prop="menuCode"
                        label-width="100"
                        v-show="menuForm.menuType == 2"
                    >
                        <el-input v-model="menuForm.menuCode" autocomplete="off" />
                    </el-form-item>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="cancel">取消</el-button>
                        <el-button type="primary" @click="submit(dialogForm)">确定</el-button>
                    </span>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<script setup>
import { Delete, Edit, Plus } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { nextTick, reactive, ref, toRaw } from 'vue';
import api from '../api';
import utils from '../utils';

const columns = reactive([
    {
        label: '菜单名称',
        prop: 'menuName',
        width: 150,
    },
    {
        label: '图标',
        prop: 'icon',
    },
    {
        label: '菜单类型',
        prop: 'menuType',
        formatter(row, column, value) {
            return {
                1: '菜单',
                2: '按钮',
            }[value];
        },
    },
    {
        label: '权限标识',
        prop: 'menuCode',
    },
    {
        label: '路由地址',
        prop: 'path',
    },
    {
        label: '组件路径',
        prop: 'component',
    },
    {
        label: '菜单状态',
        prop: 'menuState',
        formatter(row, column, value) {
            return {
                1: '正常',
                2: '停用',
            }[value];
        },
    },
    {
        label: '创建时间',
        prop: 'createTime',
        formatter: (row, column, value) => {
            return utils.formateDate(new Date(value));
        },
        width: 210,
    },
]);
const targetForm = reactive({
    targetName: '',
    targetState: '',
});
const searchLabel = [
    {
        type: 'input',
        label: '菜单名字',
        prop: 'targetName',
        placeholder: '请输入菜单名字',
    },
    {
        type: 'select',
        label: '菜单状态',
        prop: 'targetState',
        placeholder: '请选择菜单状态',
        option: [
            {
                value: 1,
                label: '正常',
            },
            {
                value: 2,
                label: '停用',
            },
        ],
    },
];
const showDialog = ref(false);
const dialogForm = ref(null);
const rules = reactive({
    menuName: [
        {
            required: true,
            message: '请输入菜单名称',
            trigger: 'blur',
        },
        {
            min: 2,
            max: 10,
            message: '长度在2-8个字符',
            trigger: 'blur',
        },
    ],
});
// 记录用户行为
const requestAction = ref('add');

// 初始化的请求以及初始化的菜单
let menuForm = reactive({
    parentId: [null],
    menuType: 1,
    menuState: 1,
});
let menuList = ref([]);

const getMenuList = async () => {
    let res = await api.getMenuList(menuForm);
    menuList.value = res;
};

// 按钮事件板块
const menuAdd = (type, row) => {
    showDialog.value = !showDialog.value;
    requestAction.value = 'add';
    if (type == 2) {
        menuForm.parentId = [...row.parentId, row._id].filter(ele => ele);
    }
};

const menuEdit = row => {
    showDialog.value = !showDialog.value;
    requestAction.value = 'edit';
    nextTick(() => {
        Object.assign(menuForm, row);
    });
};

const menuDelete = async row => {
    requestAction.value = 'delete';
    let data = { _id: row._id, action: requestAction.value };
    console.log(data);
    let res = await api.menuOperate(data);
    console.log(res);
    getMenuList();
};

const resetForm = () => {
    dialogForm.value.resetFields();
};

const submit = dialogForm => {
    dialogForm.validate(async vaild => {
        if (vaild) {
            try {
                let data = toRaw(menuForm);
                console.log(menuForm);
                data.action = requestAction.value;
                console.log(data);
                let res = await api.menuOperate(data);
                getMenuList();
                showDialog.value = !showDialog.value;
                resetForm();
                ElMessage({
                    message: '提交成功',
                    type: 'success',
                });
            } catch (error) {
                ElMessage.error('提交失败');
            }
        }
    });
};
const resetSearch = () => {
    getMenuList();
};
const cancel = () => {
    resetForm();
    showDialog.value = !showDialog.value;
};
const close = () => {
    resetForm();
};

const search = async () => {
    let target = {
        menuName: targetForm.targetName,
        menuState: targetForm.targetState,
    };
    menuList.value = await api.getMenuList(target);
};

// 调用
getMenuList();
</script>

<style lang="scss" scoped>
.userManager {
    .queryForm {
        width: 100%;
        height: 65px;
        background-color: #fff;
        border-radius: 8px;
        overflow: hidden;
        display: flex;
        align-items: center;
        padding-top: 20px;
        padding-left: 40px;
    }
    .baseTable {
        margin-top: 20px;
        background-color: #fff;
        border-radius: 8px;
        overflow: hidden;
        .action {
            width: 100%;
            height: 55px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #f1f1f1;
            button {
                margin-left: 20px;
                font-weight: bold;
            }
        }
    }
}
</style>
